/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "ESearch.h"

#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>

#include "ESearchResultParser.h"

using namespace Bio;

const QString	c_esearchUrl = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";

///////////////////////////////////////////////////////////////////////////////
//	ESearch
///////////////////////////////////////////////////////////////////////////////

/**
	@class ESearch
	@brief Implementation of the Entrez 'ESearch' function.

	Provides an interface to the NCBI Entrez 'ESearch' function.

	See http://www.ncbi.nlm.nih.gov/books/NBK25499/ for details on
	the Entrez ESearch utility.
**/

/**
	Default Constructor. Creates a new ESearch operation.
**/
ESearch::ESearch() : m_useHistory( false ), m_queryKey( -1 ), m_retStart( -1 ), m_retMax( -1 )
{
}

/**
**/
ESearch::ESearch( const QString &db, const QString &term, bool useHistory ) : m_db( db ), 
	m_term( term ), m_useHistory( useHistory ), m_queryKey( -1 ), m_retStart( -1 ), m_retMax( -1 ),
	m_count( 0 )
{
}

/**
**/
ESearch::~ESearch()
{
}

/**
**/
bool ESearch::exec()
{
	QUrl		url( c_esearchUrl );
	QUrlQuery	urlQuery;

	if( m_db.isEmpty() ){
		error( "No database specified for ESearch." );
		return false;
	}

	urlQuery.addQueryItem( "db", m_db );

	if( m_term.isEmpty() ){
		error( "No search term specified for ESearch." );
		return false;
	}

	urlQuery.addQueryItem( "term", m_term );
	
	if( m_useHistory )
		urlQuery.addQueryItem( "usehistory", "y" );

	if( m_queryKey >= 0 )
		urlQuery.addQueryItem( "query_key", QString::number( m_queryKey ) );

	if( !m_webEnv.isEmpty() )
		urlQuery.addQueryItem( "WebEnv", m_webEnv );

	if( m_retStart >= 0 )
		urlQuery.addQueryItem( "retstart", QString::number( m_retStart ) );

	if( m_retMax >= 0 )
		urlQuery.addQueryItem( "retmax", QString::number( m_retMax ) );

	// urlQuery.addQueryItem( "rettype", m_rettype );

	if( !m_field.isEmpty() )
		urlQuery.addQueryItem( "field", m_field );

	switch( m_dateType ){
	case ModDate:
		urlQuery.addQueryItem( "datetype", "mdat" );
		break;
	case PubDate:
		urlQuery.addQueryItem( "datetype", "pdat" );
		break;
	case EntrezDate:
		urlQuery.addQueryItem( "datetype", "edat" );
		break;
	default:
		break;
	}

	if( m_relDate >= 0 )
		urlQuery.addQueryItem( "reldate", QString::number( m_relDate ) );

	if( m_minDate.isValid() && m_maxDate.isValid() ){
		urlQuery.addQueryItem( "mindate", m_minDate.toString( "yyyy/MM/dd" ) );
		urlQuery.addQueryItem( "maxdate", m_maxDate.toString( "yyyy/MM/dd" ) );
	}

	ESearchResultParser	parser( *this );

	url.setQuery( urlQuery );

	return execGet( url, parser );
}

/**
**/
void ESearch::setDatabase( const QString &db )
{
	m_db = db;
}

/**
**/
QString ESearch::getDatabase() const
{
	return m_db;
}

/**
**/
void ESearch::setTerm( const QString &term )
{
	m_term = term;
}

/**
**/
QString ESearch::getTerm() const
{
	return m_term;
}

/**
**/
void ESearch::setUseHistory( bool onOff )
{
	m_useHistory = onOff;
}

/**
**/
bool ESearch::getUseHistory() const
{
	return m_useHistory;
}

/**
**/
void ESearch::setHistory( int queryKey, const QString &webEnv )
{
	m_queryKey = queryKey;
	m_webEnv = webEnv;
}

/**
**/
int ESearch::getQueryKey() const
{
	return m_queryKey;
}

/**
**/
QString ESearch::getWebEnv() const
{
	return m_webEnv;
}

/**
**/
void ESearch::setRetStart( int num )
{
	m_retStart = num;
}

/**
**/
int ESearch::getRetStart() const
{
	return m_retStart;
}

/**
**/
void ESearch::setRetMax( int num )
{
	m_retMax = num;
}

/**
**/
int ESearch::getRetMax() const
{
	return m_retMax;
}

/**
**/
void ESearch::setField( const QString &field )
{
	m_field = field;
}

/**
**/
QString ESearch::getField() const
{
	return m_field;
}

/**
**/
void ESearch::setDateType( DateType type )
{
	m_dateType = type;
}

/**
**/
Entrez::DateType ESearch::getDateType() const
{
	return m_dateType;
}

/**
**/
void ESearch::setRelDate( int num )
{
	m_relDate = num;
}

/**
**/
int ESearch::getRelDate() const
{
	return m_relDate;
}

/**
**/
void ESearch::setDateRange( const QDate &minDate, const QDate &maxDate )
{
	m_minDate = minDate;
	m_maxDate = maxDate;
}

/**
**/
QDate ESearch::getMinDate() const
{
	return m_minDate;
}

/**
**/
QDate ESearch::getMaxDate() const
{
	return m_maxDate;
}

/**
**/
int ESearch::getCount() const
{
	return m_count;
}

/**
**/
int ESearch::getNumIDs() const
{
	return m_ids.count();
}

/**
**/
QString ESearch::getID( int which ) const
{
	if( which < 0 || which >= m_ids.count() )
		return QString();

	return m_ids[which];
}

/**
**/
QStringListIterator ESearch::getIDIterator() const
{
	return QStringListIterator( m_ids );
}

/**
**/
void ESearch::setCount( int count )
{
	m_count = count;
}

/**
**/
void ESearch::addID( const QString &id )
{
	m_ids.append( id );
}
